﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using WeighClient.Model;
using WeighClient.Utils;

namespace WeighClient.Windows
{
    /// <summary>
    /// SaleInfoWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SaleInfoWindow : Window
    {
        Bill billModel;
        string labelMsg = "";
        private DispatcherTimer timer;
        private String TextProperty1 = String.Empty;
        public SaleInfoWindow(Bill bill)
        {
            InitializeComponent();
            billModel = bill;
            this.DataContext = billModel;
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += new EventHandler(timeCycle);
            timer.Start();
        }
        public void timeCycle(object sender, EventArgs e)
        {
            new Thread(() =>
            {
                Application.Current.Dispatcher.BeginInvoke(
                    new Action(() =>
                    {
                        TextProperty1 = DateTime.Now.ToString();
                        this.labelTime.Text = TextProperty1;
                    }));
            }).Start();
        }

        public void LabelMsg(string msg)
        {
            this.labelMsg = msg;
            errorLabel.Content = labelMsg;

        }

        public void UpdateBill(Bill bill)
        {
            billModel = bill;
            this.DataContext = billModel;

        }

        private void printTest(object sender, RoutedEventArgs e)
        {
            //try
            //{
                JObject reqObj = new JObject();
                reqObj.Add("type", "PrinterStatus");
                reqObj.Add("message", "备用打印机缺纸");
                SendNodeRedMessage("PostMessage/PrinterStatus", reqObj);



            //    int m_iStatus = 0;
            //    int t1 = 0x519;
            //    int p1 = 0x2015;
            //    MsPrinterUtils.SetDevVIDPID(t1, p1);
            //    m_iStatus =MsPrinterUtils.GetStatus();


            //    int t2 = 0x519;
            //    int p2 = 0x2013;
            //    MsPrinterUtils.SetDevVIDPID(t2, p2);
            //    m_iStatus = MsPrinterUtils.GetStatus();

            //    if (m_iStatus != 0)
            //    {
            //        if (m_iStatus == 7 || m_iStatus == 8)
            //        {
            //            MessageBox.Show("打印机纸张缺失");
            //            return;
            //        }
            //        MessageBox.Show("打印机异常");
            //        return ;
            //    }
            //    m_iStatus = MsPrinterUtils.GetStatusspecial();
            //    if (m_iStatus != 0)
            //    {                 
            //        MessageBox.Show("打印机异常");
            //        return;
            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show("打印机异常");
            //}
            //PrintUtils.printWeighOrder(billModel);
        }

        private void SendNodeRedMessage(String path, JObject reqObj)
        {
            try
            {
                String result = HttpPostHelper.doHttpPost(App.GetConfig().NodeRedUrl + path, reqObj.ToString());

            }
            catch (Exception ex) { }
        }
    }
}
